<?php
/**
 * User: Nicho
 * Date: 17-3-31
 * Time: AM 08:10
 */

namespace Admin\Controller;
class PayController extends AdminController
{
    //单位表
    private $unitModel;
    //缴费表
    private $payItemModel;
    //缴费配置表
    private $payItemConfModel;
    public function __construct(){
        parent::__construct();
        $this->unitModel = M('core_unit');
        $this->payItemModel = M('core_pay_item');
        $this->payItemConfModel = M('core_pay_item_config');
        $this->payItemConfModel->create();
        $this->payItemModel->create();
    }
    const MODEL_NAME = 'Pay@:';
    //缴费列表
    public function index()
    {
        if(I('keyword')){    //查询关键字进行搜索。
            $keyword = I('keyword');
            $map['title'] = array('like','%'.$keyword.'%');
            $this->assign('keyword',$keyword);
        }
        $map['pid'] = 0;
        $totalCount = M('core_item_view')->where($map)->count();
        $page = new \Think\Page($totalCount,20);
        $limit = $page->firstRow . ',' . $page->listRows . ',' . $page->totalRows;
        $list = M('core_item_view')->where($map)->limit($limit)->order('id desc')->select();
        foreach ($list as $key => $value) {
            $list[$key]['sub'] = M('core_item_view')->where('pid = '.$value['id'])->select();
        }
        $this->assign('_list',$list);
        $this->assign('_page',$page->show());
        $this->assign('sort',$sort);
        $this->meta_title = '缴费项目列表';
        $this->display(self::MODEL_NAME.'Pay/index'); 
        
    }
    //添加/编辑缴费
    public function updateBill()
    {
        if(IS_POST){
            //缴费日期判断
            if(!I('post.start_date')) $this->error('请输入起始时间!');
            if(!I('post.end_date')) $this->error('请输入结束时间!');
            //生成缴费项
            //生成缴费项目：总项
            $this->payItemModel->pid = 0;
            $this->payItemModel->create_time = time();
            $result = $this->payItemModel->add();
            //生成缴费项目：总项-config
            $this->payItemConfModel->create_time = time();
            $this->payItemConfModel->start_date = strtotime(I('start_date'));
            $this->payItemConfModel->end_date   = strtotime(I('end_date'));
            $this->payItemConfModel->start_month = I('start_month');
            $this->payItemConfModel->end_month   = I('end_month');

            $this->payItemConfModel->item = $result;
            $result2 = $this->payItemConfModel->add();
            //生成账单
            $batch = $this->build_item($result);
            
            $this->redirect('Pay/updateBillTmp',array('batch'=>$batch));
        }else{
            //单位列表
            $unit = $this->unitModel->where('status = 1 and id ='.I('unit_id'))->find();
            $this->assign('unit',$unit);
            //上次缴费项目选择
            $oldpayList = M('core_item_view')->where(' status = 1 and pid = 0 and unit = '.I('unit_id'))->order('create_time desc')->select();
            $this->assign('oldpayList',$oldpayList);
            //缴费项列表
            $payList = M('core_unit_paytype')->where('status = 1 and unit = '.I('unit_id'))->select();
            $this->assign('payList',$payList);
            //判断添加/编辑
            $this->assign('a_type',I('a_type'));
            $this->display(self::MODEL_NAME.'Pay/updateBill');
        }
    }
    //只能生成账单所用到的所有子函数--------------------------------------------------------------------------------
    //生成缴费项目
    public function build_item($result)
    {
        //生成所有缴费项目子项
        foreach (I('pay') as $key => $value) {
            $pay = explode(',', $value);
            //生成缴费项目：子项
            $data['unit'] = I('unit');
            $data['pid'] = $result;
            $data['paytype_id'] = $pay[0];
            $data['create_time'] = time();
            $result2 = $this->payItemModel->add($data);
            $item[$pay[0]] = $result2;
            
            //生成缴费项目：子项-config
            $confdata['item'] = $result2;
            $confdata['create_time'] = time();
            switch ($pay[2]) {
                case 'manage':
                    $confdata['start_month'] = I('ms');
                    $confdata['end_month'] = I('me');
                    break;
                case 'food':
                    $confdata['start_month'] = I('fs');
                    $confdata['end_month'] = I('fe');
                    break;
            }
            $confresult = $this->payItemConfModel->add($confdata);
        }
        return $this->create_bill($result,$item);
    }
    
    //创建账单
    public function create_bill($result,$item)
    {
        $batch = md5(I('title').time());
        //取得考勤配置信息
        $ucon['unit'] = I('unit');
        $uconf = M('core_unit_config')->where($ucon)->find();
        //获取用户信息，自动添加账单
        $con['m_status'] = 1;
        $con['group_id'] = 5;
        $con['unit_id'] = I('unit');
        $userList = M('core_userinfo_view')->where($con)->getField('uid',true);
        foreach ($userList as $key => $value) {
            //生成大账单
            $data['unit'] = I('unit');
            $data['uid'] = $value;
            $data['item'] = $result;
            $data['item_c'] = 0;
            $data['amount'] = 0;
            $data['create_time'] = time();
            $data['detail'] = I('detail');
            $data['batch'] = $batch;
            $onebill = M('core_pay_bill_tmp')->add($data);
            //生成子账单
            foreach (I('pay') as $k => $v) {
                $pay = explode(',', $v);
                $data2['unit'] = I('unit');
                $data2['uid'] = $value;
                $data2['item'] = $result;
                $data2['item_c'] = $item[$pay[0]];
                $data2['last_amount'] = 0;
                if($pay[2]=="manage"){
                    //生成账单辅助表
                    $this->build_balance($onebill,$pay,$value,$result);
                    if(I('old_start')){
                        $data2['last_amount'] = $this->last_balance($pay,$value,$uconf);
                    }
                }
                $data2['amount'] = $pay[1];
                $data2['create_time'] = time();
                $data2['detail'] = I('detail');
                $data2['batch'] = $batch;
                $twobill[] = $data2;
            }
        }
        M('core_pay_bill_tmp')->addAll($twobill);
        return $batch;
    }
    //名称：上次结余
    //属性：用户循环
    //级别：原子函数
    //功能：每个孩子上次出勤情况:算出每个月剩余金额
    public function last_balance($pay,$value,$uconf)
    {   
        $total = 0;
        $con2['pid'] = I('oldbill');
        $con2['mark'] = $pay[2];
        $oldbill = M('core_item_view')->where($con2)->find();

        for ($i=I('old_start');$i<=I('old_end'); $i++) { 
            $bcon['uid'] = $value;
            $bcon['month'] = $i;
            $bcon['bstate'] = 0;
            $balance = M('core_pay_bill_balance')->where($bcon)->find();
            //如果此月未结余
            if($balance){
                //孩子上次每月出勤情况
                $acon['uid']   = $value;
                $acon['month'] = date('Y-'.$i);
                $count = M('core_attendance_detail')->where($acon)->count();

                if($count>$uconf['attendance_days']){
                    $total += $balance['amount']/=2;
                    //更改状态为已结余
                    $ocon['uid'] = $value;
                    $ocon['mark'] = $pay[2];
                    $ocon['month'] = date('Y-'.I('old_start')+$i);
                    M('core_pay_bill_balance')->where($ocon)->setField('bstate',1);
                }
            }
        }
        return $total;
    }
    //生成账单辅助表
    public function build_balance($onebill,$pay,$value,$result)
    {
        for($i=I('ms');$i <=I('me');$i++) { 
            $bdata['item'] = $result;
            $bdata['unit'] = I('unit');
            $bdata['uid'] = $value;
            $bdata['bstate'] = 0;
            $bdata['amount'] = $pay[1];
            $bdata['mark'] = $pay[2];
            $bdata['month'] = $i;
            $bdata['create_time'] = I('create_time');
            $b_bill[] = $bdata;
        }
        M('core_pay_bill_balance')->addAll($b_bill);
        
    }
    //------------------------------------------------------------------------------------------
    public function updateBillTmp()

    {


        if(IS_POST){

            $con['batch'] = I('batch');
            $tmp = M('core_pay_bill_tmp')->where($con)->select();
            $result = M('core_pay_bill')->addAll($tmp);
            if($result){
                $this->success('生成成功',U('Pay/bill'));
            }else{
                $this->error('生成失败');
            }
        }else{
            if(I('keyword')){    //查询关键字进行搜索。
            $keyword = I('keyword');
            $con['nickname'] = array('like','%'.$keyword.'%');
            $this->assign('keyword',$keyword);
            }
           
              $con['item_c'] = 0;
             $con['batch'] = I('batch');

            $list = M('core_pay_bill_tmp_view')->where($con)->select();

            foreach ($list as $key => $value) {
                $list[$key]['amount'] = M('core_pay_bill_tmp_view')->where('item_c <> 0 and item = '.$value['item'].' and uid='.$value['uid'])->sum('amount');
                $list[$key]['sub'] = M('core_pay_bill_tmp_view')->where('item_c <> 0 and item = '.$value['item'].' and uid='.$value['uid'])->select();
            }
            $this->assign('_list',$list);
                   
            $this->display(self::MODEL_NAME.'Pay/updateBillTmp');
        }
    }

    // //添加/编辑缴费
    // public function updateBill()
    // {
    //     if(IS_POST){
    //         if(!I('post.start_date')) $this->error('请输入起始时间!');
    //         if(!I('post.end_date')) $this->error('请输入结束时间!');

    //         $this->payItemConfModel->start_date = strtotime(I('start_date'));
    //         $this->payItemConfModel->end_date = strtotime(I('end_date'));

    //         switch (I('a_type')) {
    //             case 'add':
    //                 $this->payItemModel->status = 1;
    //                 $this->payItemConfModel->status = 1;
    //                 $this->payItemModel->create_time = time();
    //                 $this->payItemConfModel->create_time = time();
    //                 $result = $this->payItemModel->add();
    //                 $this->payItemConfModel->item = $result;
    //                 $result2 = $this->payItemConfModel->add();
    //                 break;
    //             case 'edit':
    //                 $this->payItemModel->update_time = time();
    //                 $this->payItemConfModel->update_time = time();
    //                 $result = $this->payItemModel->where('id = '.I('iid'))->save();
    //                 $result2 = $this->payItemConfModel->where('item = '.I('iid'))->save();
    //                 break;
    //         }
    //         if($result&&$result2){
    //             $this->success('更新成功',U('index'));
    //         }else{
    //             $this->error('更新失败');
    //         }
    //     }else{
    //         //单位列表
    //         $unitList = $this->unitModel->where('status = 1')->select();
    //         $this->assign('unitList',$unitList);
    //         //为编辑使用
    //         $item = $this->payItemModel->where('id = '.I('id'))->find();
    //         $tmp = $this->payItemConfModel->where('item = '.I('id'))->find();
    //         //格式化时间
    //         $item['start_date'] = date('Y-m-d H:i:s',$tmp['start_date']);
    //         $item['end_date'] = date('Y-m-d H:i:s',$tmp['end_date']);

    //         $this->assign('data',$item);
    //         //判断添加/编辑
    //         $this->assign('a_type',I('a_type'));
    //         $this->display(self::MODEL_NAME.'Pay/updateBill');
    //     }
    // }

    //添加/编辑临时账单
    public function editBillTmp()
    {
        if(IS_POST){
            
            $data['amount'] = I('amount');
            $result = M('core_pay_bill_tmp')->where('id = '.I('tid'))->save($data);
            if($result){
                $this->success('更新成功',U('updateBillTmp',array('batch'=>I('batch'))));
            }else{
                $this->error('更新失败');
            }
        }else{
            //判断添加/编辑
            $this->assign('id',I('id'));
            $this->assign('amount',I('amount'));
            $this->assign('batch',I('batch'));
            $this->display(self::MODEL_NAME.'Pay/editBillTmp');
        }
    }
    //添加/编辑缴费
    public function updatePay()
    {
        if(IS_POST){
            if(!I('post.start_date')) $this->error('请输入起始时间!');
            if(!I('post.end_date')) $this->error('请输入结束时间!');

            $this->payItemConfModel->start_date = strtotime(I('start_date'));
            $this->payItemConfModel->end_date = strtotime(I('end_date'));

            switch (I('a_type')) {
                case 'add':
                    $this->payItemModel->status = 1;
                    $this->payItemConfModel->status = 1;
                    $this->payItemModel->create_time = time();
                    $this->payItemConfModel->create_time = time();
                    $result = $this->payItemModel->add();
                    $this->payItemConfModel->item = $result;
                    $result2 = $this->payItemConfModel->add();
                    break;
                case 'edit':
                    $this->payItemModel->update_time = time();
                    $this->payItemConfModel->update_time = time();
                    $result = $this->payItemModel->where('id = '.I('iid'))->save();
                    $result2 = $this->payItemConfModel->where('item = '.I('iid'))->save();
                    break;
            }
            if($result&&$result2){
                $this->success('更新成功',U('index'));
            }else{
                $this->error('更新失败');
            }
        }else{
            //单位列表
            $unitList = $this->unitModel->where('status = 1')->select();
            $this->assign('unitList',$unitList);
            //为编辑使用
            $item = $this->payItemModel->where('id = '.I('id'))->find();
            $tmp = $this->payItemConfModel->where('item = '.I('id'))->find();
            //格式化时间
            $item['start_date'] = date('Y-m-d H:i:s',$tmp['start_date']);
            $item['end_date'] = date('Y-m-d H:i:s',$tmp['end_date']);

            $this->assign('data',$item);
            //判断添加/编辑
            $this->assign('a_type',I('a_type'));
            $this->display(self::MODEL_NAME.'Pay/updatePay');
        }
    }

    //添加/编辑缴费子项
    public function updateSubPay()
    {
        if(IS_POST){
            switch (I('a_type')) {
                case 'add':
                    $this->payItemConfModel->status = 1;
                    $this->payItemModel->status = 1;
                    $this->payItemConfModel->create_time = time();
                    $this->payItemModel->create_time = time();
                    $result = $this->payItemModel->add();
                    $this->payItemConfModel->item = $result;
                    $result2 = $this->payItemConfModel->add();
                    break;
                case 'edit':
                    $this->payItemModel->update_time = time();
                    $this->payItemConfModel->update_time = time();
                    $this->payItemConfModel->item = I('id');
                    $result = $this->payItemModel->where('id = '.I('id'))->save();
                    $result2 = $this->payItemConfModel->where('item = '.I('id'))->save();
                    break;
            }
            if($result&&$result2){
                $this->success('更新成功',U('index'));
            }else{
                $this->error('更新失败');
            }
        }else{
            //为编辑使用
            $iConf = $this->payItemConfModel->where('id = '.I('id'))->find();
            $this->assign('data',$iConf);
            //判断添加/编辑
            $this->assign('pid',I('pid'));          //母项id，添加时使用
            $this->assign('ititle',I('ititle'));    //母项名称，添加时使用
            $this->assign('a_type',I('a_type'));    //添加、编辑判断
            $this->display(self::MODEL_NAME.'Pay/updateSubPay');
        }
    }
    //缴费配置
    public function payConf()
    {
        if(IS_POST){
            if(I('id')){
                $this->payItemConfModel->update_time = time();
                $result = $this->payItemConfModel->where('id = '.I('id'))->save();
            }else{
                $this->payItemConfModel->create_time = time();
                $result = $this->payItemConfModel->add();
            }
            if($result){
                $this->success('更新成功',U('index'));
            }else{
                $this->error('更新失败');
            }
        }else{
            $payConf = $this->payItemConfModel->where('item = '.I('id'))->find();
           
            $this->assign('data',$payConf);
            $this->assign('tid',I('id'));
            $this->assign('utitle',I('utitle'));
            $this->display(self::MODEL_NAME.'pay/payConf');
        }
    }
    // public function bill()
    // {
    //     $con = "item.unit = unit.id and item.id = itemconf.item and item.status <> -1";
    //     if(I('keyword')){
    //         $keyword = I('keyword');
    //         $con.= " and item.title like '%".I('keyword')."%' ";
    //         $this->assign('keyword',$keyword);
    //     }
    //     $totalCount = $this->payItemModel->table('dkyey_core_pay_item item,dkyey_core_unit unit,dkyey_core_pay_item_config itemconf')->where($con.' and item.pid = 0')->count();
    //     $page = new \Think\Page($totalCount,20);
    //     $limit = $page->firstRow . ',' . $page->listRows . ',' . $page->totalRows;
    //     $list = $this->payItemModel->table('dkyey_core_unit unit,dkyey_core_pay_item item,dkyey_core_pay_item_config itemconf')->where($con)->field('unit.title as utitle,item.*,itemconf.start_date,itemconf.end_date')->limit($limit)->order('item.id desc')->select();
    //     //缴费子项
    //     foreach ($list as $key => $value) {
    //         $list[$key]['sub'] = $this->payItemModel->where('dkyey_core_pay_item.status <> -1 and dkyey_core_pay_item.pid = '.$value['id'])->join('dkyey_core_pay_item_config conf ON dkyey_core_pay_item.id = conf.item')->field('dkyey_core_pay_item.*,conf.setstate')->order('dkyey_core_pay_item.id desc')->select();
    //     }
    //     $this->assign('_list',$list);
    //     $this->assign('_page',$page->show());
    //     $this->assign('sort',$sort);
    //     $this->meta_title = '缴费项目列表';
    //     $this->display(self::MODEL_NAME.'Pay/bill'); 
        
    // }
    public function bill()
    {
        if(I('keyword')){    //查询关键字进行搜索。
            $keyword = I('keyword');
            $map['reason'] = array('like','%'.$keyword.'%');
            $this->assign('keyword',$keyword);
        }
        if(I('sort')){
            $sort = I('sort');
            if($sort == 'recycle'){
                $map['status'] = -1;
                $this->assign('recycle','【回收站】');
            }
        }else{
            $sort = 'normal';
            $map['status'] = array('egt', 0);
        }            
        $map['item_c'] = 0;
        $totalCount = M('core_pay_bill_view')->where($map)->count();
        $page = new\Think\Page($totalCount,20);
        $limit = $page->firstRow . ',' . $page->listRows . ',' . $page->totalRows;
        $list = M('core_pay_bill_view')->where($map)->limit($limit)->order('id desc')->select();
        foreach ($list as $key => $value) {
            $list[$key]['amount'] = M('core_pay_bill_view')->where('item_c <> 0 and item = '.$value['item'].' and uid='.$value['uid'])->sum('amount');
            $list[$key]['sub'] = M('core_pay_bill_view')->where('item_c <> 0 and item = '.$value['item'].' and uid='.$value['uid'])->select();
        }
        $this->assign('_list',$list);
        $this->assign('_page',$page->show());
        $this->assign('sort',$sort);
        $this->meta_title = '账单列表';
        $this->display(self::MODEL_NAME.'Pay/bill'); 
        
    }
    //设置状态
     public function setStatus()
    {
        $ids = I('request.id');
        $model = I('request.model');
        if(!$ids)$this->error('无可操作数据!');
        $map['id'] = array('in',$ids);
        $method = I('request.method');
        $model_object = D($model);
        switch ($method) {
            case 'resume':
                $model_object->status = 1;
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('激活成功!');
                }else{
                    $this->error('激活失败!');
                }
                break;
            case 'forbid':
                $model_object->status = 2;
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('禁用成功!');
                }else{
                    $this->error('禁用失败!');
                }
                break;
            case 'delete':
                $model_object->status = -1;
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('删除成功!');
                }else{
                    $this->error('删除失败!');
                }
                break;
            case 'recover':
                $model_object->status = 0;
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('恢复成功!');
                }else{
                    $this->error('恢复失败!');
                }
                break;
            case 'destory':
                $model_object->status = -2;
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('彻底删除成功!');
                }else{
                    $this->error('彻底删除失败!');
                }
                break;
            default:
                $this->error('系统参数错误!');
                break;
        }
    }
    //订单列表
     public function order()
    {
        $statustitle = "暂无条件查询，所有列表内容：";
       
        if(I('keyword2')){    //查询关键字进行搜索。
            $keyword2 = I('keyword2');
            $map['order_id|order_name'] = array('like','%'.$keyword2.'%');
            $statustitle = "按订单号为".I('keyword2')."的查询:";
            $this->assign('keyword2',$keyword2);
        }
         if (I('start') && I('end')) {
            $map['create_time'] = array(between, array(strtotime(I('start')), strtotime(I('end'))));
            $statustitle        = "从" . I('start') . "到" . I('end') . "，";
        }

        if (I('keyword1')) {
           if (I('keyword1') == 1) {
                    $map['status'] = array('eq', 1);
                    $statustitle   = " " . $statustitle . "状态为已缴费的信息：";
              }else{
              
                    $map['status'] = array('eq', 2);
                    $statustitle   = " " . $statustitle . "状态为未缴费的信息：";
                  
            }

         if(I('type')){
         $map['unit'] = I('type');
           $danwei = M('core_unit')->where('id = '.I('type'))->getField('title');
           
             $statustitle = "单位ID为".$danwei."的，".$statustitle." ";
        } 
        }
        //需要查询的字段$params储存起来，避免分页出问题
        $params = array(
            'start'    => I('start'),
            'end'      => I('end'),
            'keyword'  => I('keyword'),
            'keyword1' => I('keyword1'),
            'keyword2' => I('keyword2'),
            'type'     => I('type'),
        );
        $totalCount = M('core_pay_order')->where($map)->count();
         //再次生成分页的时候会调用$params的条件
        $page = new\Think\Page($totalCount,10,$params);
        $limit = $page->firstRow . ',' . $page->listRows . ',' . $page->totalRows;
        $list = M('core_pay_order')->where($map)->limit($limit)->order('id desc')->select();
        foreach ($list as $key => $value) 
        {
         $list[$key]['unit'] = M('core_unit')->where('id = ' . $value['unit'])->getField('title');
        }
        $this->assign('statustitle',$statustitle);
        $this->assign('totalCount',$totalCount);
        $this->assign('_list',$list);
        $this->assign('_page',$page->show());
        $this->assign('sort',$sort);
        $typeList = M('core_unit')->where('status=1')->field('id,title')->select();
        $this->assign('typeList',$typeList);
        $this->meta_title = '订单列表';
        $this->display(self::MODEL_NAME.'Pay/order'); 
        
    }
    //订单统计
    public function statist()
   {
       if (I('type')){
       $time['create_time'] = array(between, array(strtotime(I('start')), strtotime(I('end'))));
       $danweiid       = M('core_unit')->where('id = '.I('type'))->getField('title');
       $zongzhangdan   = M('core_pay_order')->where(array('unit' => I('type'),$time))->count();
       $yingshou       = M('core_pay_order')->where(array('unit' => I('type'),$time))->sum("amount");
       
       $con['unit']    = I('type');
       $con['status']  = 1;
       $con['create_time'] = array(between, array(strtotime(I('start')), strtotime(I('end'))));
       $shishou        = M('core_pay_order')->where($con)->sum("amount");
       $yijiao         = M('core_pay_order')->where($con)->count();
       $this->assign('yijiao',$yijiao);
       $this->assign('yingshou',$yingshou);
       $this->assign('shishou',$shishou);
       $this->assign('danweiid',$danweiid);
       $this->assign('zongzhangdan',$zongzhangdan);
        }
       $list = M('core_pay_order')->select();
       $typeList = M('core_unit')->where('status=1')->field('id,title')->select();
       $this->assign('typeList', $typeList);
       $this->assign('_list',$list);
       $this->meta_title = '订单统计';
       $this->display(self::MODEL_NAME.'Pay/statist'); 
       
   }
   //导出
    public function output(){
          header('Content-type:text/html;charset=utf8');
           import("Org.Util.PHPExcel");
           $resultPHPExcel = new \PHPExcel(); //一定要加跟杠，不然报错 
           if(I('batch')){
               $map['batch'] = array('like','%'.I('batch').'%');
           }
           $map['status'] = array('neq',-1);
           $list = D('core_pay_bill_view')->where($map)->select();
           $resultPHPExcel->getActiveSheet()->setCellValue('A1', 'id'); 
           $resultPHPExcel->getActiveSheet()->setCellValue('B1', '单位');
           $resultPHPExcel->getActiveSheet()->setCellValue('C1', '用户'); 
           $resultPHPExcel->getActiveSheet()->setCellValue('D1', '缴费项');
           $resultPHPExcel->getActiveSheet()->setCellValue('E1', '缴费子项');
           $resultPHPExcel->getActiveSheet()->setCellValue('F1', '缴费金额'); 
           $resultPHPExcel->getActiveSheet()->setCellValue('G1', '创建时间'); 
           $resultPHPExcel->getActiveSheet()->setCellValue('H1', '更新时间'); 
           $resultPHPExcel->getActiveSheet()->setCellValue('I1', '空：未交费；1：已缴费'); 
           $resultPHPExcel->getActiveSheet()->setCellValue('J1', '0可选1必选');
           $resultPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);  
           $resultPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);  
           $resultPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);  
           $resultPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(10);  
           $resultPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(10);  
           $resultPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(10); 
           $resultPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20);  
           $resultPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(20);
           $resultPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(20); 
           $resultPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(10);    
           
           $i = 2; 
           foreach($list as $item){ 
               $resultPHPExcel->getActiveSheet()->setCellValue('A'.$i,$item['id']); 
               $resultPHPExcel->getActiveSheet()->setCellValue('B'.$i,$item['unit_title']); 
               $resultPHPExcel->getActiveSheet()->setCellValue('C'.$i,$item['nickname']); 
               $resultPHPExcel->getActiveSheet()->setCellValue('D'.$i,$item['ititle']); 
               $resultPHPExcel->getActiveSheet()->setCellValue('E'.$i,$item['ptitle']);
               $resultPHPExcel->getActiveSheet()->setCellValue('F'.$i,$item['amount']); 
               $resultPHPExcel->getActiveSheet()->setCellValue('G'.$i,date('Y-m-d h:i:s',$item['create_time']));
               $resultPHPExcel->getActiveSheet()->setCellValue('H'.$i,date('Y-m-d h:i:s',$item['update_time'])); 
               $resultPHPExcel->getActiveSheet()->setCellValue('I'.$i,$item['paystate']);
               $resultPHPExcel->getActiveSheet()->setCellValue('J'.$i,$item['setstate']);   
               $i ++; 
           }
           //设置导出文件名 
           $outputFileName = 'bill.xls'; 
           $xlsWriter = new \PHPExcel_Writer_Excel5($resultPHPExcel); 
           //ob_start(); ob_flush(); 
           header("Content-Type: application/force-download"); 
           header("Content-Type: application/octet-stream"); 
           header("Content-Type: application/download"); 
           header('Content-Disposition:inline;filename="'.$outputFileName.'"'); 
           header("Content-Transfer-Encoding: binary"); 
           header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
           header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
           header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
           header("Pragma: no-cache"); 
           $xlsWriter->save( "php://output" );
       }
       public function change_amount(){
          $last=M('core_pay_bill_tmp')->where('id = '.I('id'))->getField('last_amount');
          $data['last_amount'] = $last +=I('amount');
          $result=M('core_pay_bill_tmp')->where('id = '.I('id'))->save($data);
          echo json_encode($last);
}

}

